/*
 * Copyright (C) 2011 Johan Maasing johan at zoom.nu Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package nu.zoom.catonine.tail;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Code to keep track of the file we are tailing.
 * 
 * @author "Johan Maasing" &lt;johan@zoom.nu&gt;
 */
abstract class AbstractFileTailer extends AbstractRegularExpressionLogBlockTailer implements Tailer {
	private final Log log = LogFactory.getLog(getClass());
	private File file = null;

	@Override
	public synchronized void setFile(File file) throws IllegalArgumentException, FileNotFoundException, IOException {
		if (file == null) {
			throw new IllegalArgumentException("file may not be null");
		}
		final boolean wasTailing = isTailing();
		if (wasTailing) {
			stopAndWait();
		}
		log.trace("Setting file to: " + file);
		this.file = file;
		if (wasTailing) {
			start();
		}
	}

	@Override
	public synchronized File getFile() throws IOException {
		return this.file != null ? new File(file.getCanonicalPath()) : null;
	}

}
